import { http } from '@/utils/request';

// 文件上传响应接口
export interface FileUploadResponse {
  id: string;
  filename: string;
  originalName: string;
  url: string;
  size: number;
  mimeType: string;
  uploadedAt: string;
}

// 文件信息接口
export interface FileInfo {
  id: string;
  filename: string;
  originalName: string;
  url: string;
  size: number;
  mimeType: string;
  uploadedAt: string;
  bucket?: string;
  key?: string;
}

// 文件列表响应接口
export interface FileListResponse {
  files: FileInfo[];
  total: number;
  page: number;
  pageSize: number;
}

// 上传文件
export function uploadFile(formData: FormData) {
  return http.post<FileUploadResponse>('/api/files/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}

// 获取文件列表
export function getFileList(params?: {
  page?: number;
  pageSize?: number;
  search?: string;
  type?: string;
}) {
  return http.get<FileListResponse>('/api/files', params);
}

// 获取文件信息
export function getFileInfo(fileId: string) {
  return http.get<FileInfo>(`/api/files/${fileId}`);
}

// 删除文件
export function deleteFile(fileId: string) {
  return http.delete<void>(`/api/files/${fileId}`);
}

// 批量删除文件
export function batchDeleteFiles(fileIds: string[]) {
  return http.delete<void>('/api/files/batch', { fileIds });
}

// 获取文件下载链接
export function getFileDownloadUrl(fileId: string, expiresIn?: number) {
  return http.get<{ url: string }>(`/api/files/${fileId}/download`, { expiresIn });
}

// 获取文件预览链接
export function getFilePreviewUrl(fileId: string) {
  return http.get<{ url: string }>(`/api/files/${fileId}/preview`);
}
